package com.bbs.toutiao.controller.client;

import cn.hutool.core.lang.Assert;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.bbs.toutiao.aspect.SysLog;
import com.bbs.toutiao.entity.UserEntity;
import com.bbs.toutiao.exception.BaseException;
import com.bbs.toutiao.exception.ExceptionEnum;
import com.bbs.toutiao.service.client.impl.ClientUserServiceImpl;
import com.bbs.toutiao.utils.Jwt;
import com.bbs.toutiao.utils.MsgCodeUtil;
import com.bbs.toutiao.utils.Result;
import com.bbs.toutiao.utils.SMSUtil;
import com.bbs.toutiao.vo.client.UserPhonePasswordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * 描述: 前台用户登录注册
 * 作者： zhangBin
 * 时间： 2023/10/8 10:35
 * 版本： 1.0
 */

@Slf4j
@RestController
@RequestMapping("/api/client")
public class ClientUserController {

    @Resource
    ClientUserServiceImpl userService;


    /**
     * 发送手机验证码
     * @param phone
     * @return
     */
    @SysLog("用户获取了手机验证码")
    @GetMapping("/getCaptcha/{phone}")
    public Result getCaptcha(@PathVariable(name = "phone") String phone) {
        try {
            Assert.notBlank(phone);
            System.out.println(phone);
            String captcha = MsgCodeUtil.getCode(phone);
//            SMSUtil smsUtil = new SMSUtil();
//            smsUtil.sendSMS(phone,captcha); //发送短信验证码
            return Result.success("验证码已发送，注意查收", captcha);
        } catch (IllegalArgumentException e) {
            throw new BaseException(ExceptionEnum.NOT_NULL);
        }
    }

    /**
     * 账号或者手机号和密码登录
     *
     * @param lrVo
     * @return
     */
    @PostMapping("/loginByAccountAndPassword")
    public Result login(@RequestBody UserPhonePasswordVo lrVo,HttpServletResponse response) {
        try {
//            log.info(lrVo.toString());
            Assert.notBlank(lrVo.getPassword());
        } catch (Exception e) {
            throw new BaseException(ExceptionEnum.NOT_NULL);
        }
        UserEntity user = userService.login(lrVo, "ACCOUNT");
        if (user.getStatus() > 0) {
            return Result.error(400, "您的账号已被封禁");
        } else {
            String token = Jwt.getToken(user);
            response.setHeader("token", token);
            return Result.success("登陆成功", user);
        }
    }

    /**
     * 手机号和验证码登录
     *
     * @param lrVo
     * @return
     */
    @PostMapping("/loginByPhoneAndCaptcha")
    public Result loginByPhone(@RequestBody UserPhonePasswordVo lrVo, HttpServletResponse response) {
        MsgCodeUtil.startCleanerCache(); //开启清理
        try {
            Assert.notBlank(lrVo.getCaptcha());
            Assert.notBlank(lrVo.getPhone());
        } catch (Exception e) {
            throw new BaseException(ExceptionEnum.NOT_NULL);
        }
        if (MsgCodeUtil.booleanCode(lrVo.getPhone(), lrVo.getCaptcha())) {
            UserEntity user = userService.login(lrVo, "PHONE");
            String token = Jwt.getToken(user);
            response.setHeader("token", token);
            return Result.success("登陆成功", user);
        }
        return Result.error(404, "验证码错误");
    }

    //前台用户查询主页信息
    @GetMapping("/getUserInfoById")
    public Result getUserInfo(@RequestHeader String token){
        log.info("客户端获取了用户详情");
        int userId;
        try {
            JWT jwt = JWTUtil.parseToken(token);
            userId = Integer.parseInt(jwt.getPayload("userId").toString());
        } catch (Exception e) {
            throw new BaseException(ExceptionEnum.TOKEN_FONT_ERROR);
        }
        return Result.success("查询成功",userService.selectUserInfo(userId));
    }

}
